﻿using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using LinqToAmazonSL;
using SilverlightApplication.LanguageResources;
using SilverlightApplication.UIControls;
using SilverlightApplication.UIControls.Carousel;
using System.Windows.Media;

namespace SilverlightApplication
{
    public partial class Page : UserControl
    {
        SearchPanels searchPanels;
        SearchController sc;
        MagnifierControl mc;
        Carousel carousel;
       
        public ObservableCollection<AmazonItem> DisplayableItems;

        public Page()
        {
            InitializeComponent();
            this.KeyUp += new KeyEventHandler(onKeyPress);

            if (System.Globalization.CultureInfo.CurrentCulture.TwoLetterISOLanguageName == "es")
            {
                LanguageLoader.SetLanguage(Languages.Spanish);
            }
            else
            {
                LanguageLoader.SetLanguage(Languages.English);
            }

            LoadComponents();
        }

        void LoadComponents()
        {
            this.Loaded += new RoutedEventHandler(Page_Loaded);
            ItemPanel itemPanel = new ItemPanel();
            StatusBar statusBar = new StatusBar();
            Logo logo = new Logo();
            WaitingForResults.WaitingForResults wfr = new WaitingForResults.WaitingForResults();
            wfr.Visibility = Visibility.Collapsed;
            searchPanels = new SearchPanels(wfr);
            searchPanels.PanelSearchType = SearchType.Book;
            searchPanels.setItemPanel(itemPanel);
            itemPanel.SetPanelsReference(searchPanels);
            LayoutRoot.Children.Add(searchPanels);
            LayoutRoot.Children.Add(itemPanel);
            LayoutRoot.Children.Add(statusBar);
            LayoutRoot.Children.Add(logo);
            LayoutRoot.Children.Add(wfr);
            Canvas.SetZIndex(logo, 0);
            Canvas.SetZIndex(searchPanels, 500);

            sc = new SearchController();
            searchPanels.SearchController = sc;
            LayoutRoot.Children.Add(sc);
            Canvas.SetZIndex(sc, -12);

            mc = new MagnifierControl();
            mc.VerticalAlignment = VerticalAlignment.Center;
            mc.HorizontalAlignment = HorizontalAlignment.Center;
            mc.Opacity = 0.4;
            Canvas.SetZIndex(mc, -11);
            LayoutRoot.Children.Add(mc);
            TranslateTransform t = new TranslateTransform();
            t.Y = 20;
            mc.RenderTransform = t;
            sc.MagnifierCtrl = mc;
        }

        void Page_Loaded(object sender, RoutedEventArgs e)
        {
            carousel = new Carousel();
            this.LayoutRoot.Children.Add(carousel);
            DisplayableItems = new ObservableCollection<AmazonItem>();
            carousel.DisplayableItems = DisplayableItems;
            Canvas.SetZIndex(carousel, -10);
            searchPanels.setCarousel(carousel);
            sc.Carousel = carousel;
            
        }

        void onKeyPress(object sender, KeyEventArgs e)
        {
            if (carousel != null && !carousel.DetailedDisplaying)
            {
                if (e.Key == Key.Left)
                {
                    carousel.Move(Carousel.Direction.Left, 1);
                }
                else if (e.Key == Key.Right)
                {
                    carousel.Move(Carousel.Direction.Right, 1);
                }
            }
        }
    }
}
